import 'package:distribution_mall_flutter/constant/constant.dart';
import 'package:distribution_mall_flutter/constant/point_constant.dart';
import 'package:distribution_mall_flutter/entity/category.dart';
import 'package:distribution_mall_flutter/pages/home/widgets/tab.dart';
import 'package:distribution_mall_flutter/provider/category/category.dart';
import 'package:distribution_mall_flutter/provider/home/waterfall.dart';
import 'package:distribution_mall_flutter/utils/common_third.dart';
import 'package:distribution_mall_flutter/utils/firebase/firebase_analytics_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class TabState {
  bool? showButton;
  int activeIndex = 0;
  int? tabSlide; // 0 左滑 1右滑
  TabState({this.showButton,  this.activeIndex = 0, this.tabSlide});

  TabState.initial()
      : showButton = true;

  TabState copyWith({
    bool? showButton,
    int? activeIndex,
    int? tabSlide,
  }) =>
      TabState(
          showButton: showButton ?? this.showButton,
          tabSlide: tabSlide,
          activeIndex: activeIndex ?? this.activeIndex);
}

final tabProvider =
    StateNotifierProvider.autoDispose<TabViewModel, TabState>((ref) {
  return TabViewModel(
      movePage: (int i) {
        ref.read(waterfallProvider.notifier).movePages(i);
      },
      categoryList: ref.watch(categoryProvider).categoryList);
});

class TabViewModel extends StateNotifier<TabState> {
  List<Category>? categoryList;
  Function(int i)? movePage;
  TabViewModel({this.movePage, this.categoryList}) : super(TabState.initial());

  void tabIndex(int index) {
    if (index == state.activeIndex) {
      return;
    }
    state = state.copyWith(activeIndex: index);
  }

  void clickTab(int index) {
    if (index == state.activeIndex) return;
  }

  void changeTab(int index, {String tabName = '', bool enableActive = true}) {
    if (index == state.activeIndex) return;
    // FirebaseAnalyticsUtils
    //     .logEvent(name: PointConstant.hpCategory, parameters: {'tag': tabName});
    tabIndex(index);
  }

  void changeSlide(int? i) {
    if (i == null) return;
    state = state.copyWith(tabSlide: i);
  }

  void changeConIndex(int? i, [TabController? tabController]) {
    if (i == 0 && state.activeIndex > 0) {
      int activeIndex = state.activeIndex - 1;
      if (tabController != null) {
        // tabController.index = activeIndex;
        tabController.animateTo(activeIndex);
      }
      if (movePage is Function) {
        movePage!(0);
      }
      changeTab(activeIndex, tabName: tabName[activeIndex]);
    } else if (i == 1 && state.activeIndex < tabName.length - 1) {
      int activeIndex = state.activeIndex + 1;
      if (tabController != null) {
        // tabController.index = activeIndex;
        tabController.animateTo(activeIndex);
      }
      if (movePage is Function) {
        movePage!(1);
      }
      changeTab(activeIndex, tabName: tabName[activeIndex]);
    }
    Future.delayed(const Duration(milliseconds: 500), () {
      state = state.copyWith(tabSlide: null);
    });
  }
}
